﻿package com.xuanque.mahjongDice.ui{
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.Timer;
	
	import com.xuanque.mahjongDice.control.GameAction;
	
	public class CountdownClock extends MovieClip {
		
		private var clockTimer:Timer;
		private var __time_txt:TextField;
		
		//public var _time_txt:TextField;
		
		public function CountdownClock() {
			__time_txt = timer_txt;
			__time_txt.selectable = false;
			__time_txt.autoSize = "center";
			//clockTimer = new Timer(1,1);
		}
		
		public function getClockTimer():Timer{
		  return clockTimer;
		}
		
		public function setTimer(delay:int,repeatCount:int = 60):void{
			if(clockTimer!=null){
			  if(clockTimer.running){
		       clockTimer.stop();
		       __time_txt.text = "";
		     }
			}
			clockTimer = new Timer(delay,repeatCount);
			__time_txt.text = String(repeatCount);
		}
		public function startReady(){
		        clockTimer.addEventListener(TimerEvent.TIMER, clockTick);
				clockTimer.addEventListener(TimerEvent.TIMER_COMPLETE, GameAction.getInstance().readyTimeOut); //准备超时
				clockTimer.start();
		}
		public function startTrun(){;
		        clockTimer.addEventListener(TimerEvent.TIMER, clockTick);
				clockTimer.start();
		}
		public function startClock(func:String):void {
			if(func=="timerOut"){
				clockTimer.addEventListener(TimerEvent.TIMER, clockTick);
				clockTimer.addEventListener(TimerEvent.TIMER_COMPLETE, GameAction.getInstance().priorityTimerOut);
				clockTimer.start();
			}else{
				clockTimer.addEventListener(TimerEvent.TIMER, clockTick);
				clockTimer.addEventListener(TimerEvent.TIMER_COMPLETE, GameAction.getInstance().OutServerTimerOut);
				clockTimer.start();
			}
			
		}
		
		public function stopClock():void{
			if(clockTimer!=null){
			  if(clockTimer.running){
		       clockTimer.stop();
		       __time_txt.text = "";
		     }
			}
		}
		public function clockTick(event:TimerEvent) {
			showClock();
		}
		
		public function showClock():void {
			__time_txt.text = String(clockTimer.repeatCount - clockTimer.currentCount);
		}
			
		
		public function clockEnd(event:TimerEvent) {
			__time_txt.text = "0";
		}
	}
}
